package com.company.dcm.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtils {
    public static boolean uploadFile(MultipartFile file, String uploadDir) throws IOException {
        // 确保目标目录存在
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 上传文件
        File destFile = new File(uploadDir + file.getOriginalFilename());
        try {
            file.transferTo(destFile);  // 保存文件到相对路径指定的目录
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    // FileUtils工具类新增方法
    public static String saveToTemp(MultipartFile file) throws IOException {
        Path tempDir = Paths.get("/usr/share/nginx/html/detection/");
        Path target = tempDir.resolve(file.getOriginalFilename());
        file.transferTo(target);  // 同步操作
        return target.toString();
    }
}
